﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class PopupBLL
  {
    public List<Popup> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Popups.ToList();
      }
    }
    public List<Popup> GetAll(bool IsEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Popups.Where(p => p.IsEnable).ToList();
      }
    }

    public Popup GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Popups.Where(p => p.Id == id).FirstOrDefault();
      }
    }


    public Popup GetPopup()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Popups.Where(p => p.IsEnable == true).FirstOrDefault();
      }
    }
    //Enable

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Popup obj = data.Popups.Where(p => p.Id == id).FirstOrDefault();
        obj.IsEnable = !obj.IsEnable;

        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Popup obj = data.Popups.Where(p => p.Id == id).FirstOrDefault();
        obj.IsEnable = isEnable;

        data.SubmitChanges();
      }
    }
    public void Add(Popup obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Popups.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    public void Update(Popup obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Popup popup = data.Popups.Where(p => p.Id == obj.Id).FirstOrDefault();

        if (popup != null)
        {
          popup.Image = obj.Image;
          popup.IsEnable = obj.IsEnable;
          popup.Link = obj.Link;
          popup.Name = obj.Name;
          data.SubmitChanges();
        }
      }
    }

    public void Delete(int id)
    {
      using(var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Popup obj = data.Popups.Where(p => p.Id == id).FirstOrDefault();
        if (obj != null)
          data.Popups.DeleteOnSubmit(obj);

        data.SubmitChanges();
      }
    }
  }
}
